package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.ProductInfo;
import com.suyuan.entity.PurchaseInfo;
import com.suyuan.entity.TransitInfo;
import com.suyuan.service.NewTransitBatchService;
import com.suyuan.service.ProductService;
import com.suyuan.service.TransitBatchInfoService;
import com.suyuan.service.TransitInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.vo.NewTransitBatchVO;
import com.suyuan.vo.TransitBatchInfoVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 *  物流管理
 *
 */
@Controller
@RequestMapping(value = "/admin/transitInfo")
public class TransitInfoController {
    @Resource(name = "transitInfoService")
    private TransitInfoService transitInfoService;
    //@Resource(name = "transitBatchInfoService")
    //private TransitBatchInfoService transitBatchInfoService;
    @Resource(name = "newTransitBatchService")
    private NewTransitBatchService newTransitBatchService;
    @Resource(name = "productService")
    private ProductService productService;

    /**
     * 列表
     * @return
     */
    @RequestMapping( value = "/list")
    public String list(Integer batchId, String compName , Page page , ModelMap model){
        PageInfo info = transitInfoService.list(batchId,compName,page);
        model.addAttribute("page",info);
        model.addAttribute("batchId",batchId);
        model.addAttribute("compName",compName);
        return "transport_list";
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return null;
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            transitInfoService.delete(Integer.parseInt(id));
        }
        return null;
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        //批次编号
        List<NewTransitBatchVO> batches = newTransitBatchService.list();
        //生猪集合
        List<PurchaseInfo> pigs = transitInfoService.getPigs();
        //产品编号
        List<ProductInfo> products = productService.findAll();
        model.addAttribute("batches",batches);
        model.addAttribute("pigs",pigs);
        model.addAttribute("products",products);
        return "transport_add";
    }

    /**
     * 保存
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(TransitInfo transitInfo){
        int code = transitInfoService.save(transitInfo);
        if(code == 0){
            return JsonMessage.error("添加失败");
        }
        return JsonMessage.success("添加成功");
    }

    /**
     * 编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        TransitInfo transit = transitInfoService.findById(id);
        List<NewTransitBatchVO> batches = newTransitBatchService.list();
        List<PurchaseInfo> pigs = transitInfoService.getPigs();
        model.addAttribute("batches",batches);
        model.addAttribute("pigs",pigs);
        model.addAttribute("transit",transit);
        return "transport_edit";
    }

    /**
     * 更新
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TransitInfo transit){
        int code = transitInfoService.update(transit);
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }
}
